package com.yuke.cloud.service.omc.service.impl;

import com.yuke.cloud.common.core.support.BaseService;
import com.yuke.cloud.service.api.pmc.dto.ProdSpecInfoDto;
import com.yuke.cloud.service.api.pmc.service.PmcFeignApi;
import com.yuke.cloud.service.omc.dao.OdsTeamGoodsMapper;
import com.yuke.cloud.service.omc.dto.CommissionGoodsDto;
import com.yuke.cloud.service.omc.dto.OdsTeamGoodsDto;
import com.yuke.cloud.service.omc.entity.OdsTeamGoods;
import com.yuke.cloud.service.omc.service.OdsTeamGoodsService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * Created by zn on 2020/10/05.
 */
@Service
@Transactional
public class OdsTeamGoodsServiceImpl extends BaseService<OdsTeamGoods> implements OdsTeamGoodsService {
    @Resource
    private OdsTeamGoodsMapper odsTeamGoodsMapper;
    @Resource
    private PmcFeignApi pmcFeignApi;

    @Override
    public  Map<String, Integer> selectCountGoods(OdsTeamGoods odsTeamGoods){
        return odsTeamGoodsMapper.selectCountGoods(odsTeamGoods);
    }

    @Override
    public List<OdsTeamGoodsDto> selectTeamGoodsByCondition(OdsTeamGoodsDto odsTeamGoodsDto) {
        List<OdsTeamGoodsDto> odsTeamIntentDtoList = odsTeamGoodsMapper.selectTeamGoodsByCondition(odsTeamGoodsDto);
        return odsTeamIntentDtoList;
    }

    @Override
    public List<OdsTeamGoodsDto> getExtraInfo(List<OdsTeamGoodsDto> odsTeamGoodsDtoList){
        List<Long> specIds = odsTeamGoodsDtoList.stream().map(OdsTeamGoodsDto::getSpecId).collect(Collectors.toList());
        List<ProdSpecInfoDto> prodSpecInfoDtos  = pmcFeignApi.getSpecDtosBySpecIds(specIds);
        if (prodSpecInfoDtos == null || prodSpecInfoDtos.size() < 1){
            return odsTeamGoodsDtoList;
        }
        for (OdsTeamGoodsDto item : odsTeamGoodsDtoList){

            for(ProdSpecInfoDto prodSpecInfoDto : prodSpecInfoDtos){
                if (item.getSpecId().equals(prodSpecInfoDto.getSpecId())){
                    item.setPicUrl(prodSpecInfoDto.getBigPic());
                    item.setSpecName(prodSpecInfoDto.getSpecName());
                }
            }

        }

        return odsTeamGoodsDtoList;
    }

    @Override
    public List<OdsTeamGoodsDto> teamIntentCommission(Long shopId,Byte checkStatus){
        List<OdsTeamGoodsDto> odsTeamIntentDtoList = odsTeamGoodsMapper.teamIntentCommission(shopId,checkStatus);

        odsTeamIntentDtoList = getExtraInfo(odsTeamIntentDtoList);
        return odsTeamIntentDtoList;
    }

    @Override
    public List<CommissionGoodsDto> teamGoodsCommission(Long shopId,String dateCreateTime,Byte checkStatus) {
        List<CommissionGoodsDto> odsTeamIntentDtoList = odsTeamGoodsMapper.teamGoodsCommission(shopId, dateCreateTime,checkStatus);
        List<Long> specIdList = odsTeamIntentDtoList.stream().map(CommissionGoodsDto::getSpecId).collect(Collectors.toList());
        List<ProdSpecInfoDto> prodSpecInfoDtos  = pmcFeignApi.getSpecDtosBySpecIds(specIdList);
        if (prodSpecInfoDtos == null || prodSpecInfoDtos.size() < 1){
            return odsTeamIntentDtoList;
        }
        for (CommissionGoodsDto item : odsTeamIntentDtoList){

            for(ProdSpecInfoDto prodSpecInfoDto : prodSpecInfoDtos){
                if (item.getSpecId().equals(prodSpecInfoDto.getSpecId())){
                    item.setPicUrl(prodSpecInfoDto.getBigPic());
                    item.setSpecName(prodSpecInfoDto.getSpecName());
                }
            }

        }

        return odsTeamIntentDtoList;
    }
}
